﻿using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;

namespace ttlib
{
    public struct PicOption
    {
        public uint borderLeft;
        public uint borderTop;
        public uint borderRight;
        public uint borderBottom;
        public int pivotX;
        public int pivotY;
        public static PicOption? FromJson(JObject json)
        {
            try
            {
                PicOption o = new PicOption();
                if (json.ContainsKey("border"))
                {
                    var bstr = json["border"].ToString().Split(new char[] { ',', ' ' },StringSplitOptions.RemoveEmptyEntries);
                    o.borderLeft = uint.Parse(bstr[0]);
                    o.borderTop = uint.Parse(bstr[1]);
                    o.borderRight = uint.Parse(bstr[2]);
                    o.borderBottom = uint.Parse(bstr[3]);
                }
                if (json.ContainsKey("pivot"))
                {
                    var bstr = json["pivot"].ToString().Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
                    o.pivotX = int.Parse(bstr[0]);
                    o.pivotY = int.Parse(bstr[1]);
                }
                return o;
            }
            catch(Exception err)
            {
                return null;
            }
        }
        public static JObject ToJson(PicOption option)
        {
            JObject json = new JObject();
            json["border"] = option.borderLeft + "," + option.borderTop + "," + option.borderRight + "," + option.borderBottom;
            json["pivot"] = option.pivotX + "," + option.pivotY;
            return json;
        }
    }
    public class SpriteInfo
    {
        public string name;
        public PicOption option;
        public uint uvx;
        public uint uvy;
        public uint uvwidth;
        public uint uvheight;   
    }

}
